<?php
/**
 * 文件名：video.mod.php
 * 版本号：1.0
 * 作者：mrasong
 * 功能描述：视频模块
 */
if(!defined('IN_JISHIGOU'))
{
    exit('invalid request');
}

class ModuleObject extends MasterObject{
	var $ID = 0;

	function ModuleObject($config)
	{
		$this->MasterObject($config);
		if(isset($this->Get['id'])){
			$this->ID = (int)$this->Get['id'];
		}elseif(isset($this->Post['id'])){
			$this->ID = (int)$this->Post['id'];
		}
		
		Load::logic('topic');
		$this->TopicLogic = new TopicLogic();
		
				$this->my = array();
		if (MEMBER_ID < 1) {
			$this->Messager("请先登录");	
		}
		
		$this->my = $this->TopicLogic->GetMember(MEMBER_ID);
		
		$this->Execute();
	}

	function Execute()
	{
		ob_start();
		
		switch($this->Code)
		{
			case "show":
				$this->show();
				break;
			case "fav":
				$this->fav();
				break;
			case "fav_add":
				$this->fav_add();
				break;
			case "fav_del":
				$this->fav_del();
				break;
			case "edit":
				$this->edit();
				break;
			case "edit_save":
				$this->edit_save();
				break;
			case "del":
				$this->del();
				break;
			case "comm_add":
				$this->comm_add();
				break;
			case "comm_del":
				$this->comm_del();
				break;
			default:
				$this->Main();
				break;
		}
		$Contents=ob_get_clean();
		$this->ShowBody($Contents);
	}

	function Main()
	{	
		$page_size = 12;
		$page = empty($this->Get['page']) ? 1 : $this->Get['page'];

		$query_link = 'index.php?mod=video';
		$sql = "select count(*) as `total_video` from `".TABLE_PREFIX."video` where userId = {$_COOKIE['MEMBER_ID']}";
		$query = $this->DatabaseHandler->Query($sql);
        extract($query->GetRow());
		$total_page = ceil($total_video/$page_size);
		$page = $page>$total_page ? $total_page : $page;
		
		$page_arr = pagebrand($total_video,$page_size,$query_link,array('return'=>'array',));
		
		$sql = "select * from `".TABLE_PREFIX."video` where userId =".$_COOKIE['MEMBER_ID']." {$page_arr['limit']}";

	 	$query = $this->DatabaseHandler->Query($sql);
	    $video_list=array(); 
		while($row = $query->GetRow())
		{	
			$sqlaa = "select count(*) `count_comm` from `".TABLE_PREFIX."video_comment` where videoId = {$row['id']}";
			$queryA = $this->DatabaseHandler->Query($sqlaa);
			extract($queryA->GetRow());
			$row['comm_count'] = $count_comm;
			$video_list[]=$row;
		}		
		
		include(ROOT_PATH . './templates/app/video_index.php');
	}
	
	function edit(){
		$vInfo = array(
			'id'	=> '' ,
			'videoTitle'=> '',
			'picUrl'	=> '',
			'videoUrl'	=> '',
			'content'	=> '',
			'hits'		=> 0
		);
		$vid = $this->Get['vid'];	
		if( !empty($vid) ){
			$vid = intval($vid);
			$sql = "select * from  `".TABLE_PREFIX."video` where id = $vid and userId = {$_COOKIE['MEMBER_ID']}";
			$query = $this->DatabaseHandler->Query($sql);
			while ($row = $query->GetRow()){
				$vInfo = $row;
			}
		}

		include(ROOT_PATH . './templates/app/video_edit.php');
	}
	
	function del(){
		$vid = $this->Get['vid'];	
		$sql = "delete from `".TABLE_PREFIX."video` where id = $vid and userId ={$_COOKIE['MEMBER_ID']}";
		$query = $this->DatabaseHandler->Query($sql);	
		$html = "<script type='text/javascript'>alert('删除成功！');location.href='index.php?mod=video';</script>";
		echo $html;
	}
	
	function fav(){

		$page_size = 12;
		$page = empty($this->Get['page']) ? 1 : $this->Get['page'];

		$query_link = 'index.php?mod=video&code=fav';
		$sql = "select count(*) as `total_video` from `".TABLE_PREFIX."video` where id in (select videoId from  `".TABLE_PREFIX."video_fav` where userId ={$_COOKIE['MEMBER_ID']})";
		$query = $this->DatabaseHandler->Query($sql);
        extract($query->GetRow());
		$total_page = ceil($total_video/$page_size);
		$page = $page>$total_page ? $total_page : $page;
		
		$page_arr = pagebrand($total_video,$page_size,$query_link,array('return'=>'array',));
		
		$sql = "select * from `".TABLE_PREFIX."video` where id in (select videoId from  `".TABLE_PREFIX."video_fav` where userId = {$_COOKIE['MEMBER_ID']} )  {$page_arr['limit']}";

	 	$query = $this->DatabaseHandler->Query($sql);
	    $video_list=array(); 
		while($row = $query->GetRow())
		{	
			$sqlaa = "select count(*) `count_comm` from `".TABLE_PREFIX."video_comment` where videoId = {$row['id']}";
			$queryA = $this->DatabaseHandler->Query($sqlaa);
			extract($queryA->GetRow());
			$row['comm_count'] = $count_comm;
			$video_list[]=$row;
		}
	
		include(ROOT_PATH . './templates/app/video_fav.php');
	
	}
	
	
	function fav_add(){
		$vid = $this->Get['vid'];
		$sql_exist = "select * from `".TABLE_PREFIX."video_fav` where videoId='' and userId = {$_COOKIE['MEMBER_ID']}";
	 	$query_exist = $this->DatabaseHandler->Query($sql_exist);
		$row_exist = $query_exist->GetRow();
		if( empty( $row_exist ) ){
			$sql = "insert into  `".TABLE_PREFIX."video_fav` ( `userId`, `videoId` ) values ( {$_COOKIE['MEMBER_ID']}, $vid )";
			$this->DatabaseHandler->Query($sql);
			$html = "收藏成功！";
		}else{
			$html = "您已经收藏过该视频！";
		}
		$html = "<script type='text/javascript'>alert('$html');location.href='index.php?mod=video&code=fav';</script>";
		echo $html;
	
	}
	
	function fav_del(){
		$vid = $this->Get['vid'];	
		$sql = "delete from `".TABLE_PREFIX."video_fav` where videoId = $vid and userId ={$_COOKIE['MEMBER_ID']}";
		$query = $this->DatabaseHandler->Query($sql);		
		$html = "<script type='text/javascript'>alert('删除成功！');location.href='index.php?mod=video&code=fav';</script>";
		echo $html;
	}
	
	
	function show(){
		$vid = $this->Get['vid'];
		$sql = "select * from `".TABLE_PREFIX."video` where id = $vid";
		$query = $this->DatabaseHandler->Query($sql);
		while ($row = $query->GetRow()){
			$vInfo = $row;
		}
		
		$sql_prev = "select id from `".TABLE_PREFIX."video` where id < $vid limit 1";
		$query_prev = $this->DatabaseHandler->Query($sql_prev);
		$row_prev = $query_prev->GetRow();
		$vInfo['id_prev'] = empty($row_prev['id']) ? $vid :  $row_prev['id'];
		
				
		$sql_next = "select id from `".TABLE_PREFIX."video` where id > $vid limit 1";
		$query_next = $this->DatabaseHandler->Query($sql_next);
		$row_next = $query_next->GetRow();
		$vInfo['id_next'] = empty($row_next['id']) ? $vid :  $row_next['id'];
		
		
		
		
		$sqlu = "update `".TABLE_PREFIX."video` set hits=hits+1 where id = $vid";
		$this->DatabaseHandler->Query($sqlu);
		
		$sqlt = "select count(*) as `total_comment` from `".TABLE_PREFIX."video_comment` where videoId = $vid";
		$queryt = $this->DatabaseHandler->Query($sqlt);
        extract($queryt->GetRow());
		$vInfo['comm'] = $total_comment;
		
		$sqlc = "select a.*, b.username, b.nickname,b.face from `".TABLE_PREFIX."video_comment` as a left join `".TABLE_PREFIX."members` as b on b.uid = a.userId  where videoId = $vid order by a.addTime desc";

		$queryc = $this->DatabaseHandler->Query($sqlc);
		while ($rowc = $queryc->GetRow()){
			$comm_list[] = $rowc;
		}

		include(ROOT_PATH . './templates/app/video_show.php');
	
	}
	
	function edit_save(){
		$vInfo = $this->Post;
		
		if( empty($vInfo['pic_url']) ){
			if($image = $_FILES['pic']['name']){
				$savename= uniqid().strtolower(strstr($_FILES['pic']['name'], '.'));
				$s_pic= dirname(dirname(__FILE__))."/upload/video/".$savename;
				if(is_uploaded_file($_FILES['pic']['tmp_name'])){
					if(!move_uploaded_file($_FILES['pic']['tmp_name'], $s_pic)){
						echo '上传失败';
						exit;
					}
				}else{
					echo 'problem!';
					exit;
				}
			}
			$pic_url = $savename;
		}else{
			$pic_url = $vInfo['pic_url'];
		}
		
		if( empty($vInfo['vid']) ){
			$sql = "insert into  `".TABLE_PREFIX."video` ( `userId`, `videoTitle`, `picUrl`, `videoUrl`, `content`, `addTime`, `hits` ) 
				values( ".$_COOKIE['MEMBER_ID'].", '{$vInfo['video_title']}', '$pic_url', '{$vInfo['video_url']}', '{$vInfo['content']}', '".date('Y-m-d H:i:s')."', 0 )";
			$html = "添加视频成功";
		}else{
			$sql = "update `".TABLE_PREFIX."video` set  `videoTitle` = '{$vInfo['video_title']}', `picUrl` = '$pic_url', `videoUrl` = '{$vInfo['video_url']}',
				`content` = '{$vInfo['content']}', `addTime` = '".date('Y-m-d H:i:s')."'  where id={$vInfo['vid']} and userId ={$_COOKIE['MEMBER_ID']}";
			$html = "视频修改成功";
		}

		$query = $this->DatabaseHandler->Query($sql);		
		$html = "<script type='text/javascript'>alert('$html');location.href='index.php?mod=video';</script>";
		echo $html;
	}
	
	function comm_add(){	
		$vid = $this->Get['vid'];
		$sql = "insert into  `".TABLE_PREFIX."video_comment` (`userId`, `videoId`, `content`, `addTime`) values ( {$_COOKIE['MEMBER_ID']}, $vid, '{$this->Post['content']}','". date('Y-m-d H:i:s') ."' )";
		$query = $this->DatabaseHandler->Query($sql);		
		$html = "<script type='text/javascript'>alert('留言成功！');location.href='index.php?mod=video&code=show&vid=$vid';</script>";
		echo $html;
	}
	
	
	function comm_del(){	
		$id = $this->Get['id'];
		$vid = $this->Get['vid'];
		$sql = "delete from `".TABLE_PREFIX."video_comment` where id=$id";
		$query = $this->DatabaseHandler->Query($sql);
		$html = "<script type='text/javascript'>alert('删除评论成功！');location.href='index.php?mod=video&code=show&vid=$vid';</script>";
		echo $html;
	}
	
	
	
}

?>
